using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class Lesson10_InvokeCSharpEvents : MonoBehaviour
{
    private Rigidbody body;
    private Vector3 dir;
    public Bullet bullet;
    PlayerInput input;
    void Start()
    {
        input = this.GetComponent<PlayerInput>();
        body = this.GetComponent<Rigidbody>();

        input.onActionTriggered += onActionTrigger;
    }

    public void OnJump(InputAction.CallbackContext context)
    {
        body.AddForce(Vector3.up * 200);
    }

    public void OnFire(InputAction.CallbackContext context)
    {
        //鼠标位置的射线检测
        RaycastHit info;
        if (Physics.Raycast(Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue()), out info))
        {
            //得到子弹飞出去的向量
            Vector3 point = info.point;
            point.y = this.transform.position.y;
            Vector3 dir = point - this.transform.position;
            //创建子弹飞出去
            Instantiate(bullet, this.transform.position, Quaternion.LookRotation(dir));
        }
    }

    //需要获取值 这种函数  需要注意 只会在改变时进入函数
    public void OnMove(InputAction.CallbackContext context)
    {
        dir = context.ReadValue<Vector2>();
        dir.z = dir.y;
        dir.y = 0;
    }

    public void onActionTrigger(InputAction.CallbackContext context)
    {
        switch (context.action.name)
        {
            case "Fire":
                OnFire(context);
                break;
            case "Jump":
                OnJump(context);
                break;
            case "Move":
                OnMove(context);
                break;
        }
    }

    void Update()
    {
        body.AddForce(dir * 10);
    }
}
